package com.contract.management.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.List;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;

import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;

public class PdfUtils {
	
	/**
	 * 将pdf转换为图片，放置在pdf同目录下。
	 * @param pdf
	 * @return 图片文件名
	 */
	/*
	public static String toPic(File pdf) {
		String fileName = pdf.getAbsolutePath();
		String picName = fileName.substring(0, fileName.lastIndexOf("."));
		try {
			PDDocument doc = PDDocument.load(pdf);
			PDFRenderer renderer = new PDFRenderer(doc);
			List<BufferedImage> list = new ArrayList<>();
			for(int page = 0; page< doc.getNumberOfPages(); page++) {
				BufferedImage img = renderer.renderImageWithDPI(page, 300);
				list.add(img);
			}
			doc.close();
			int heightTotal = 0;
			for(int j =0; j < list.size(); j++) {
				heightTotal += list.get(j).getHeight();
			}
			int heightCurr = 0;
			BufferedImage img = new BufferedImage(list.get(0).getWidth(), heightTotal, BufferedImage.TYPE_INT_RGB);
			Graphics2D g2d = img.createGraphics();
			for(int j = 0; j< list.size(); j++) {
				g2d.drawImage(list.get(j), 0, heightCurr, null);
				heightCurr += list.get(j).getHeight();
			}
			g2d.dispose();
			File jpg = new File(picName + ".jpg");
			ImageIO.write(img, "jpg", jpg);
			return jpg.getName();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	*/
	
	/**
	 * 将pdf转换为图片，放置在pdf同目录下。
	 * @param pdf
	 * @return 图片文件名,可能多个，以“;”分开。
	 */
	public static String toPic(File pdf) {
		String filePath = pdf.getAbsolutePath();
		String picPath = filePath.substring(0, filePath.lastIndexOf("."));
		String fileName = pdf.getName();
		String picName = fileName.substring(0, fileName.lastIndexOf("."));
		
		StringBuilder sb = new StringBuilder();
		try (FileInputStream is = new FileInputStream(pdf)) {
			PDFParser parser = new PDFParser(is);
			parser.parse();
			PDDocument doc = parser.getPDDocument();
			List<PDPage> pages = doc.getDocumentCatalog().getAllPages();
			for(int i = 0; i < pages.size(); i++) {
				PDPage page = pages.get(i);
				
				String saveFileName = picPath + "_" + i + ".jpg";
				if(i != 0) sb.append(";");
				sb.append(picName + "_" + i + ".jpg");
				BufferedImage imgTemp  = page.convertToImage();  
				Iterator<ImageWriter> it = ImageIO.getImageWritersBySuffix("jpg");  
				ImageWriter writer = (ImageWriter) it.next();   
		        ImageOutputStream imageout = ImageIO.createImageOutputStream(new FileOutputStream(saveFileName));  
		        writer.setOutput(imageout);  
		        writer.write(new IIOImage(imgTemp, null, null));  
		        imageout.close();
			}
			doc.close();
			return sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static void main(String[] args) {
		System.out.println(toPic(new File("e:/加计扣除合同.pdf")));
	}
}
